package org.example.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.mapper.EmpExprMapper;
import org.example.mapper.EmpMapper;
import org.example.pojo.*;
import org.example.service.EmpLogService;
import org.example.service.EmpService;
import org.example.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogService empLogService;

        /*---------------原始分页查询实现-------------------------
     * 基于PageHelper插件分页查询
     * @param page 页码
     * @param pageSize 每页记录数
     * @return
    @Override
    public PageResult<Emp> page(Integer page, Integer pageSize) {
//        1.调用mapper接口，查询总记录数
        Long total = empMapper.count();

//        2.调用mapper接口，查询结果列表
        Integer start = (page - 1) * pageSize;
        List<Emp> rows = empMapper.list(start, pageSize);

//        3.封装结果 PageResult
        return new PageResult<Emp>(total,rows);

//    }*/

    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
//        1.设置分页参数
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());

//        2.执行查询
        List<Emp> empList = empMapper.page(empQueryParam);

//        3.解析查询结果，并封装
        Page<Emp> pageResult = (Page<Emp>)empList;
        return new PageResult<Emp>(pageResult.getTotal(),pageResult.getResult());

    }

    @Transactional(rollbackFor = {Exception.class})  //事务管理 - 默认出现运行时异常RuntimeException,回滚事务
    @Override
    public void save(Emp emp) throws Exception{
        try {
//        1.保存员工基本信息
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.insert(emp);

//        2.保存员工工作经历信息
            List<EmpExpr> exprList = emp.getExprList();
            if(!CollectionUtils.isEmpty(exprList)){
    //            遍历集合，为empId赋值
                exprList.forEach(expr -> expr.setEmpId(emp.getId()));
                empExprMapper.insertBatch(exprList);
            }
        } finally {
            //        记录操作日志
            EmpLog empLog = new EmpLog(null,LocalDateTime.now(),"新增员工信息:"+emp);
            empLogService.insertLog(empLog);
        }
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void delete(List<Integer> ids) {
//        1.批量删除员工基本信息
        empMapper.deleteByIds(ids);

//        2.批量删除员工工作经历信息
        empExprMapper.deleteByEmpIds(ids);

    }

    @Override
    public Emp getInfo(Integer id) {
        return empMapper.getById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Emp emp) {
//        1.根据ID修改员工的基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);

//        2.根据ID修改员工的工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
//        2.1 先根据员工ID删除员工工作经历信息
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
        if(!CollectionUtils.isEmpty(exprList)){
            //            遍历集合，为empId赋值
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            empExprMapper.insertBatch(exprList);
        }

    }

    @Override
    public List<Emp> list() {
        List<Emp> empList = empMapper.list();
        return empList;
    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp e = empMapper.getByUsernameAndPassword(emp);
        if(e != null){

            Map<String, Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("username",e.getUsername());
            String jwt = JwtUtils.generateToken(claims);
            return new LoginInfo(e.getId(),e.getUsername(),e.getPassword(),e.getName(),jwt);
        }
        return null;
    }


}
